package com.qinxueit.yueshu.api;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.StrUtil;
import com.qinxueit.yueshu.entity.Article;
import com.qinxueit.yueshu.entity.Chapter;
import com.qinxueit.yueshu.utils.UserAgentUtil;
import com.qinxueit.yueshu.utils.UuidUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

public class parseHtml {
    public static Article getArticleTitle(String parseUrl) throws Exception {

        String userAgent = UserAgentUtil.randomUserAgent();
        Document document = Jsoup.connect(parseUrl).
                userAgent(userAgent)
                .ignoreHttpErrors(true).followRedirects(true).timeout(100000).ignoreContentType(true).get();

        // 分类
        String bookType = document.head().select("meta").eq(10).attr("content");
        //System.out.println(bookType);

        Element infoEle = document.getElementById("info");
        //System.out.println(infoEle);
        // 小说名
        String bookName = infoEle.getElementsByTag("h1").text();
        //System.out.println(bookName);
        // 作者
        String author = StrUtil.sub(infoEle.getElementsByTag("p").eq(0).text(), 7, 30);

        // 更新日期
        String updateTime = StrUtil.sub(infoEle.getElementsByTag("p").eq(2).text(), 5, 30);
        LocalDateTime localDateTime = LocalDateTimeUtil.of(DateUtil.parse(updateTime));

        // 简介
        String briefIntro = document.getElementById("intro").getElementsByTag("p").text();

        // 封面
        String frontCover = document.getElementById("fmimg").getElementsByTag("img").attr("src");


        Article article = new Article();
        String uuid = UuidUtil.generateShortUuid();
        article.setArticleId(uuid);
        article.setBookName(bookName);
        article.setAuthor(author);
        article.setCreateTime(localDateTime);
        article.setUpdateTime(localDateTime);
        article.setBookUrl(parseUrl);
        article.setFrontCover(frontCover);
        article.setBriefIntro(briefIntro);
        article.setBookType(bookType);
        return article;
    }

    public static List<Chapter> getChapterList(Article article) throws IOException {
        //System.out.println(article);

        ArrayList<Chapter> contentList = new ArrayList<>();

        String userAgent = UserAgentUtil.randomUserAgent();
        Document document = Jsoup.connect(article.getBookUrl()).
                userAgent(userAgent)
                .ignoreHttpErrors(true).followRedirects(true).timeout(100000).ignoreContentType(true).get();

        Element listEle = document.getElementById("list");
        Elements ddEles = listEle.getElementsByTag("dd");


        for (Element ddEle : ddEles) {

            String userAgent2 = UserAgentUtil.randomUserAgent();
            String href = ddEle.getElementsByTag("a").attr("href");
            // System.out.println(href);
            Document document1 = Jsoup.connect(href).
                    userAgent(userAgent2)
                    .ignoreHttpErrors(true).followRedirects(true).timeout(100000).ignoreContentType(true).get();
            //System.out.println(document1);
            String chapterContent = document1.getElementById("content").getElementsByTag("p").text();
            //System.out.println(chapterContent);

            Chapter chapter = new Chapter();
            chapter.setArticleId(article.getArticleId());
            chapter.setChapterContent(chapterContent);
            chapter.setCreateTime(LocalDateTime.now());
            chapter.setUpdateTime(LocalDateTime.now());
            contentList.add(chapter);
            System.out.println(contentList);
        }

        return contentList;
    }
}
